package fr.umlv.m2.recipes.ideas;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
import fr.umlv.m2.recipes.ideas.bean.RecipeBean;
import fr.umlv.m2.recipes.ideas.sqlite.db.objects.RecipesDB;

/**
 * Create the Recipe Activity which show the detail of a recipe
 * @author Christophe Chillan
 *
 */
public class RecipeView extends Activity {
	private ImageView myImg;
	private TextView titre;
	private TextView description;

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		TextView textview = new TextView(this);
	    textview.setText("Vous etes dans les RecipeView");
	    setContentView(R.layout.recipeview);
	    
	   
	   
	    Bundle extras = getIntent().getExtras(); 
	    if(extras !=null)
	    {
	    	final RecipeBean recipe = (RecipeBean) getIntent().getExtras().getSerializable("recipe");
	    	RatingBar ratingBar = (RatingBar)findViewById(R.id.ratingbar);
	 	    ratingBar.setEnabled(false);
	    	myImg = (ImageView) findViewById(R.id.img);
	    	myImg.setImageResource(getResources().getIdentifier(recipe.getPictureLink(), null, getPackageName()));
	    	titre = (TextView) findViewById(R.id.titre);
	    	description = (TextView) findViewById(R.id.description);
	    	description.setMovementMethod(new ScrollingMovementMethod());
	    	titre.setText(recipe.getName());
	    	ratingBar.setRating(recipe.getDifficulty());
	    	description.setText(recipe.getDescription());
	    	Button button = (Button) findViewById(R.id.addToFavorite);
	    	button.setOnClickListener(addToFavoriteListener(recipe));
	    }
	    
	}

	private OnClickListener addToFavoriteListener(final RecipeBean recipe) {
		return new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				recipe.setBookmark((short)1);
				RecipesDB.getRecipeDao().update(RecipesDB.getDatabase(), recipe, recipe.getId());
				Toast.makeText(RecipeView.this, "recette : "+recipe.getName()+" ajoutée au favoris", Toast.LENGTH_LONG).show();
				
			}
		};
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.mainmenu, menu);
		return true;
	}

	/**
	 * Use to Define the Option Menu
	 */
	public boolean onOptionsItemSelected(MenuItem item) {
		// On regarde quel item a été cliqué grâce à son id et on déclenche une
		// action
		switch (item.getItemId()) {
			case R.id.home:
				startActivity(new Intent(this, MenuTabWidget.class));
				return true;
			case R.id.about:
				startActivity(new Intent(this, AboutView.class));
				return true;
			default:
		        return super.onOptionsItemSelected(item);
			}
		
	}
}
